package com.iqismart.shorturl.controller.api.user;

import com.alibaba.fastjson.JSONObject;
import com.iqismart.shorturl.cache.UrlCountCache;
import com.iqismart.shorturl.service.UserService;
import com.iqismart.shorturl.vo.*;
import com.iqismart.shorturl.vo.base.Result;
import com.iqismart.shorturl.websocket.Message;
import com.iqismart.shorturl.websocket.MyWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2019/10/14 01:32
 */
@Controller
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    @ResponseBody
    public Result<LoginResponse> login(@RequestBody LoginRequest request){
        return new Result(userService.login(request));
    }

    @RequestMapping("logout")
    @ResponseBody
    public Result logout(HttpServletRequest request, HttpServletResponse response){

       //浏览器端清除cookie即可
        return new Result();
    }

    @RequestMapping("register")
    @ResponseBody
    public Result<LoginResponse> register(@RequestBody RegisterRequest request){
        return new Result(userService.register(request));
    }

    @PostMapping("userInfo")
    @ResponseBody
    public Result userInfo(@RequestBody UserInfoRequest request){
        userService.userInfo(request);
        return new Result();
    }

    @GetMapping("userInfo")
    @ResponseBody
    public Result getUserInfo(){
        UserInfoResponse response = userService.getUserInfo();
        return new Result(response);
    }

    @RequestMapping("resetPassword")
    @ResponseBody
    public Result resetPassword(@RequestBody ResetPasswordRequest request){
        userService.resetPassword(request);
        return new Result();
    }

    @RequestMapping("apiKey")
    @ResponseBody
    public Result<String> apiKey(){
        return new Result(userService.apiKey());
    }

    @GetMapping("online")
    @ResponseBody
    public Result online(){
        List<JSONObject> onlineUserList = new ArrayList<>();
        MyWebSocket.webSocketSet.forEach(w->{
            JSONObject u = new JSONObject();
            u.put("uid",w.getUserId()>0?w.getUserId():"无");
            u.put("username",w.getUserName());
            try {
                u.put("lastActive", Message.formatTimeDifference(w.getLastActive()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            onlineUserList.add(u);
        });

        int count  = Integer.parseInt(UrlCountCache.map.get("randomOnlineUserCount")) - onlineUserList.size();
        List<UserInfoResponse>  userList = userService.getNewUserList(count);
        for (UserInfoResponse userInfoResponse : userList) {
            JSONObject u = new JSONObject();
            u.put("uid",userInfoResponse.getId());
            u.put("username",userInfoResponse.getUsername());
            try {
                u.put("lastActive", Message.formatTimeDifference(System.currentTimeMillis() - (Integer.MAX_VALUE%userInfoResponse.getId()) + 300));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(onlineUserList.size() < 50){
                onlineUserList.add(u);
            }
        }

        return new Result(onlineUserList);
    }

    @GetMapping("new")
    @ResponseBody
    public Result newUser(){
        List<UserInfoResponse>  userList = userService.getNewUserList();
        return new Result(userList);
    }

}
